/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package engine;

/**
 *
 * @author Alexx
 */
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
public class ComboBoxTable extends JFrame
{
 JComboBox cb; 
 Vector model;
public ComboBoxTable()
{
  final String[] names = {"First Name", "Last Name", "Favorite Color"};
  final Object[][] data = { //даннные для JTable
       {"Mark", "Andrews", "red"},
       {"Tom", "Ball", "blue"}
        }; 
  model = new Vector(); //данные для JComboBox
  model.addElement( new Item(1, "red" ) );
  model.addElement( new Item(2, "blue" ) );
  model.addElement( new Item(3, "green" ) );
  model.addElement( new Item(4, "gray" ) );  

  TableModel dataModel = new AbstractTableModel() {
            public int getColumnCount() { return names.length; }   
            public int getRowCount() { return data.length;}
            public Object getValueAt(int row, int col) {return data[row][col];}           
            public String getColumnName(int column) {return names[column];}
            public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
            public boolean isCellEditable(int row, int col) {return true;}
            public void setValueAt(Object aValue, int row, int column) {                
                if (column==2){
                   Item itv=(Item) model.get(cb.getSelectedIndex());
		   System.out.println(itv.getId());
		}
                data[row][column] = aValue;             
                
            }

         };

  JTable tableView = new JTable(dataModel);
  TableColumn colorColumn = tableView.getColumn("Favorite Color");
   cb=new JComboBox();
   Item it; 
   Iterator itr=model.iterator(); 
   while (itr.hasNext()){ 
        it =(Item) itr.next();
        cb.addItem(it.toString());
       }

       colorColumn.setCellEditor(new DefaultCellEditor(cb));
       JScrollPane scrollpane = new JScrollPane(tableView);     
       scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));
       scrollpane.setPreferredSize(new Dimension(430, 200));
       getContentPane().add(scrollpane);
       pack();
       setVisible(true);

}
class Item{
  private int id;
  private String description;
  public Item(int id, String description)
  {
   this.id = id;
   this.description = description;
  }

  public int getId()
  {
   return id;
  }

  public String getDescription()
  {
   return description;
  }
  public String toString()
  {
    return description;
  }
}

public static void main(String[] args)
{
 JFrame frame = new ComboBoxTable();
 frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
 frame.pack();
 frame.setVisible( true );
}

} 
